﻿using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.DataAccess;
using BySoft.Server.DataAccess.UserCode;
using OneSoft.DataAccess;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BySoft.Server.Business.UserCode
{
    public partial class BsBOMBusiness
    {
        public static DataTable SelectList(string BOMID, string WorkOrderNo, string ProductName, string cpFrom, string cpEnd, int pageIndex, int PageSize, out int m_count)
        {
            return BsBOMDataAccess.SelectList(BOMID, WorkOrderNo, ProductName, cpFrom, cpEnd, pageIndex, PageSize, out m_count);
        }

        public static DataTable SelectDialogProduct(string BOMID, string ProductID, string ProductName, string userid, int pageIndex, int PageSize, out int m_count)
        {
            return BsBOMDataAccess.SelectDialogProduct(BOMID, ProductID, ProductName, userid, pageIndex, PageSize, out m_count);
        }

        public static bool Del(string strCode)
        {
            bool isResult = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    DataAccessCommon.Delete(strCode, "BOMID", "BS_BOMKnife");
                    DataAccessCommon.Delete(strCode, "BOMID", "BS_BOMDetail");
                    DataAccessCommon.Delete(strCode, "BOMID", "BS_BOM");
                    broker.Commit();
                }
                catch (Exception ex)
                {
                    isResult = false;
                    broker.Rollback();
                    throw ex;
                }
            }
            return isResult;
        }

        public static DataTable SelectDialogMaterial(string MaterialID, string MaterialName, int pageIndex, int PageSize, out int m_count)
        {
            return BsBOMDataAccess.SelectDialogMaterial(MaterialID, MaterialName, pageIndex, PageSize, out m_count);
        }

        public static DataTable SelectDialogKnife(string KnifeID, string KnifeName, int pageIndex, int PageSize, out int m_count)
        {
            return BsBOMDataAccess.SelectDialogKnife(KnifeID, KnifeName, pageIndex, PageSize, out m_count);
        }

        public static bool IsBOMIDExists(string BOMID)
        {
            bool isResult = false;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    string str = @"select * from BS_BOM where BOMID=@BOMID";
                    DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                    dpc.AddWithValue("@BOMID", BOMID);
                    DataTable d = DataAccessCommon.Select(str, dpc).Tables[0];
                    if (d != null && d.Rows.Count > 0) 
                    {
                        isResult = true;
                    }
                    broker.Commit();
                }
                catch (Exception ex)
                {
                    isResult = true;
                    broker.Rollback();
                    throw ex;
                }
            }
            return isResult;
        }

        public static bool SaveOrUpdate(BsBomEntity entity, DataTable BOMMaterialDT, DataTable BOMKnifeDT, string type)
        {
            bool istag = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                broker.BeginTransaction();
                try
                {
                    if (type == "Save")
                    {
                        DataAccessCommon.Insert(entity, broker);
                    }
                    else if (type == "Update")
                    {
                        DataAccessCommon.UpdateOnly(entity, broker);
                    }
                    //原料
                    if (BOMMaterialDT != null && BOMMaterialDT.Rows.Count > 0)
                    {
                        foreach (DataRow drItem in BOMMaterialDT.Rows)
                        {
                            BsBomdetailEntity DetailEntity = new BsBomdetailEntity();
                            DetailEntity.BOMID = entity.BOMID;
                            DetailEntity.IsDirect = CommonMethod.GetString(drItem["IsDirect"]);
                            DetailEntity.MaterialID = CommonMethod.GetString(drItem["MaterialID"]);
                            DetailEntity.MMNumber = CommonMethod.ConvertToDecimal(drItem["MMNumber"]);
                            DetailEntity.SingleWeight = CommonMethod.ConvertToDecimal(drItem["SingleWeight"]);
                            DetailEntity.SingleNum = CommonMethod.ConvertToDecimal(drItem["SingleNum"]);
                            string strIsNew = CommonMethod.GetString(drItem["IsNew"]);
                            string strIsDel = CommonMethod.GetString(drItem["IsDel"]);
                            if (strIsNew == "1" && strIsDel != "1")
                            {
                                DataAccessCommon.Insert(DetailEntity, true, broker);
                            }
                            if (strIsNew != "1" && strIsDel != "1")
                            {
                                DetailEntity.BOMDetailID = CommonMethod.ConvertToDecimal(drItem["BOMDetailID"]);
                                DataAccessCommon.UpdateOnly(DetailEntity, broker);
                            }
                            if (strIsNew != "1" && strIsDel == "1")
                            {
                                //删除明细
                                ClsCommDA.Del("BS_BOMDetail", "BOMDetailID", CommonMethod.GetString(drItem["BOMDetailID"]), broker);
                            }
                        }
                    }
                    //刀具
                    if (BOMKnifeDT != null && BOMKnifeDT.Rows.Count > 0)
                    {
                        foreach (DataRow drItem in BOMKnifeDT.Rows)
                        {
                            BsBomknifeEntity DetailEntity = new BsBomknifeEntity();
                            DetailEntity.BOMID = entity.BOMID;
                            DetailEntity.KnifeID = CommonMethod.GetString(drItem["KnifeID"]);
                            DetailEntity.Nums = CommonMethod.ConvertToDecimal(drItem["Nums"]);
                            DetailEntity.KnifeStatus = CommonMethod.GetString(drItem["KnifeStatus"]);
                            string strIsNew = CommonMethod.GetString(drItem["IsNew"]);
                            string strIsDel = CommonMethod.GetString(drItem["IsDel"]);
                            if (strIsNew == "1" && strIsDel != "1")
                            {
                                DataAccessCommon.Insert(DetailEntity, true, broker);
                            }
                            if (strIsNew != "1" && strIsDel != "1")
                            {
                                DetailEntity.BOMKnifeID = CommonMethod.ConvertToDecimal(drItem["BOMKnifeID"]);
                                DataAccessCommon.UpdateOnly(DetailEntity, broker);
                            }
                            if (strIsNew != "1" && strIsDel == "1")
                            {
                                //删除明细
                                ClsCommDA.Del("BS_BOMKnife", "BOMKnifeID", CommonMethod.GetString(drItem["BOMKnifeID"]), broker);
                            }
                        }
                    }

                    broker.Commit();
                }
                catch
                {
                    istag = false;
                    broker.Rollback();
                    throw;
                }
                return istag;
            }
        }

        public static DataTable GetBOMDetailByID(string id)
        {
            return BsBOMDataAccess.GetBOMDetailByID(id);
        }

        public static DataTable GetBOMKnifeByID(string id)
        {
            return BsBOMDataAccess.GetBOMKnifeByID(id);
        }
    }
}
